package com.mc.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.web.multipart.MultipartFile;

import com.mc.model.file;
import com.mc.utils.XMLParserUtil;
/**
 * 该类是一个对文件进行操作的一个工具类
 * 该类是用懒汉式实现的单例类
 * @author Administrator
 *
 */
public class FileUtil {
	private static final String  ORIGNALSTR = "abcdefghijklmnopqrstuvwxyz";
	private static final int LENGTH = 8; 
	private static final String FILEWEBPATH = "ziliao";
	private file f= null;
    private static FileUtil fileUtil=new FileUtil();
    private FileUtil(){}
    public static FileUtil getInstance()
    {
    	if(fileUtil==null)
    	{
    		fileUtil=new FileUtil();
    	}
    	return fileUtil;
    }
    /**
     * 
     * @param filename  原文件名
     * @return 得到源文件的后缀
     */
	public String getFilesuffix(String filename){
		if(filename.contains(".")){
			String [] suffixs = filename.split("\\.");
			String suffix = suffixs[suffixs.length-1];
			return suffix;
		}
		return "";
	}
	/*
	 * filePath 需要查找的文件夹的绝对路径
	 * return   返回该文件夹下面的所有文件
	 */
	public  List<file> readfile(String filepath) {
		List<file> realFileNames=new ArrayList<file>();
		
		String realFileName=null;
		try {

			File file = new File(filepath);
			if (!file.isDirectory()) {

			} else if (file.isDirectory()) {
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					File readfile = new File(filepath + "\\" + filelist[i]);
					if (!readfile.isDirectory()) {
						realFileName=readfile.getName();
						//用正则表达式提取jpg gif bmp 图像文件
						 String reg="(?i).+?\\.(jpg|gif|bmp)";
						if(realFileName.matches(reg))
						{
							 f= new file();
							f.setFileName(realFileName);
							f.setFileSrc(readfile.getAbsolutePath());
							realFileNames.add(f);
						
						}						
					} else if (readfile.isDirectory()) {
						readfile(filepath + "\\" + filelist[i]);
					}
				}

			}

		} catch (Exception e) {
			System.out.println("readfile()   Exception:" + e.getMessage());
		}
		return realFileNames;
	}
	/**
	 * 
	 * @param files 要进行上传的图片文件
	 * @return 上传照片的路径
	 */
		public String uploadFiels(MultipartFile[] files) {
			Map<String,String> filepro = XMLParserUtil.getInstance().ParserUtil("file.xml");
			
			String imagepath="";
			for (MultipartFile file : files) {
				if (file.isEmpty())
					continue;
				else {
					imagepath = upload(
							file,
							StringUtil2.getInstance()
									.getPhysicsPath(filepro.get("filepath")));					
				}
			}
			
			return  imagepath;
		}
		public String upload(MultipartFile file,String savePath)
		  {
			  String imgPath = null;
			  String filename = file.getOriginalFilename();
			  String filesuffix = StringUtil2.getInstance().getSuffix(filename);
			  String realfilename = StringUtil2.getInstance().getRandomStr(ORIGNALSTR, LENGTH) + "." + filesuffix;	  
			 // System.out.println("savePath---" + savePath);
			 File f = new File(savePath,realfilename);
			  if(!f.exists())
			  {
				  f.mkdirs();
			  }
			  try {
				file.transferTo(f);
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			  imgPath = realfilename;
			  return imgPath;
		  }
}
